1 00:00:00,390 --> 00:00:07,110 I want to set up a simple queue service that can take several messages created rapidly on the server 2 00:00:07,110 --> 00:00:10,530 side and display them individually on the client side. 3 00:00:10,530 --> 00:00:15,570 For instance, when we enter the game, we may trigger three or four events, but we want to show the 4 00:00:15,570 --> 00:00:18,350 messages in a way that the user can actually read it. 5 00:00:18,360 --> 00:00:21,810 We don't want to just spam them with a bunch of windows that pop up. 6 00:00:21,810 --> 00:00:29,370 So I'm going to create this simple queue service in its own game and I'm going to move it to my zombie 7 00:00:29,370 --> 00:00:29,850 game. 8 00:00:29,850 --> 00:00:31,560 Let's take a look at what we got. 9 00:00:33,670 --> 00:00:35,050 Hey, I hit the play button. 10 00:00:35,530 --> 00:00:40,110 All of a sudden, we're going to get three messages that are just created instantly right there. 11 00:00:40,120 --> 00:00:44,260 And now you can see that the player has time to read them. 12 00:00:44,260 --> 00:00:46,960 And we could slow this down if we wanted to. 13 00:00:46,990 --> 00:00:48,940 I thought this would be really useful. 14 00:00:49,060 --> 00:00:52,320 I created an empty base plate for my Q service. 15 00:00:52,330 --> 00:00:53,330 There's nothing in it. 16 00:00:53,350 --> 00:01:00,040 I'm going to start off by going to replicate, add storage, hit the plus, add a remote event because 17 00:01:00,040 --> 00:01:02,380 we're going to be talking from server to client. 18 00:01:02,620 --> 00:01:09,460 I'm going to call the remote event info message r E, and we're going to need something to generate 19 00:01:09,460 --> 00:01:11,090 messages for our test. 20 00:01:11,110 --> 00:01:18,400 Let's go to server script service, hit the plus, add a script and I'll call this message gen. 21 00:01:19,630 --> 00:01:20,860 There we go. 22 00:01:22,000 --> 00:01:26,650 Let's get rid of our print statement and get a variable for replicated storage. 23 00:01:26,770 --> 00:01:34,510 Call it RS game, get service, replicated storage, and then in replicated storage, I'm going to get 24 00:01:34,510 --> 00:01:39,730 my info message remote event or info message re. 25 00:01:40,000 --> 00:01:41,560 It is in replicated storage. 26 00:01:41,560 --> 00:01:49,530 We'll do a wait for child info message r a now type in game players to get the player service. 27 00:01:49,540 --> 00:01:52,360 We'll get the player added event from the player service. 28 00:01:52,360 --> 00:01:56,560 Connect that to an anonymous function with a player gets passed in. 29 00:01:57,280 --> 00:02:03,130 We could do a print statement just so we know that we've we haven't made any mistakes up to this point. 30 00:02:03,160 --> 00:02:11,500 We'll do player added and then perhaps wait 5 seconds just so that you you can you can view stuff in 31 00:02:11,500 --> 00:02:13,690 the world before things start firing off. 32 00:02:13,750 --> 00:02:15,160 You get in there All right. 33 00:02:15,160 --> 00:02:17,020 And then I'm going to do a prep statement. 34 00:02:17,020 --> 00:02:21,760 I'm going to say print sending messages. 35 00:02:21,760 --> 00:02:25,480 So I know that if I'm waiting too long after that, something has failed. 36 00:02:25,480 --> 00:02:27,820 If I'm waiting at all after that, something has failed. 37 00:02:28,240 --> 00:02:31,810 I'm going to make a for loop, for I equals 1 to 3. 38 00:02:31,810 --> 00:02:34,900 We'll do three messages in steps of one. 39 00:02:34,900 --> 00:02:38,290 Do make a variable for your message. 40 00:02:38,680 --> 00:02:44,410 And what I say I call this something like this is message number. 41 00:02:44,740 --> 00:02:49,210 Put a space speech quote dot, dot I. 42 00:02:49,210 --> 00:02:54,460 So we're going to concatenate the number on our string with these two dots. 43 00:02:55,900 --> 00:02:57,310 Now, what should I do? 44 00:02:58,210 --> 00:02:58,990 Let's print it out. 45 00:02:58,990 --> 00:02:59,650 Print. 46 00:03:02,010 --> 00:03:08,430 Server side message and let's do MSG. 47 00:03:09,260 --> 00:03:10,900 What else we will fire. 48 00:03:10,910 --> 00:03:12,350 Let's fire our remote event. 49 00:03:12,350 --> 00:03:13,990 So we're going to fire the remote event. 50 00:03:14,000 --> 00:03:16,580 Nothing's there to catch it yet, but we'll go ahead and fire it. 51 00:03:17,600 --> 00:03:18,890 Fire client. 52 00:03:19,340 --> 00:03:21,230 You're going to need the player to pass it to. 53 00:03:21,410 --> 00:03:23,600 And the message. 54 00:03:24,080 --> 00:03:25,900 All right, let's try this out. 55 00:03:25,910 --> 00:03:31,070 We should only be able to see stuff on the server side now because we didn't do anything with the client 56 00:03:31,070 --> 00:03:31,740 side. 57 00:03:31,760 --> 00:03:33,170 I'll hit the play button. 58 00:03:33,470 --> 00:03:36,200 We'll go to view output. 59 00:03:37,710 --> 00:03:40,290 So I get my output window view output. 60 00:03:40,320 --> 00:03:41,310 There it is. 61 00:03:41,640 --> 00:03:42,840 And there's our messages. 62 00:03:42,840 --> 00:03:45,120 So we had our player added, sending messages. 63 00:03:45,120 --> 00:03:46,680 We've got three messages. 64 00:03:48,420 --> 00:03:52,830 Let's go ahead and turn off our game and work on the client side stuff now. 65 00:03:52,830 --> 00:03:54,390 So I'll just turn this off. 66 00:03:56,340 --> 00:03:58,620 And I'm going to close this window right here. 67 00:03:58,650 --> 00:04:03,990 Let's go to our starter guy, because we're going to do this stuff and we're going to display messages 68 00:04:03,990 --> 00:04:05,190 with the with the guy. 69 00:04:05,220 --> 00:04:09,690 So let's hit the plus sign screen gooey and then on the screen gooey. 70 00:04:09,690 --> 00:04:16,290 I'm going to do a local script and the local script, I'm going to call this process 71 00:04:18,180 --> 00:04:20,220 MSG script. 72 00:04:23,090 --> 00:04:28,570 On the client side, we are also going to have to get a variable for our info message. 73 00:04:28,580 --> 00:04:30,020 RE That's the gateway. 74 00:04:30,020 --> 00:04:32,510 So we have to connect on both sides. 75 00:04:32,600 --> 00:04:40,430 Let's get a variable for replicated storage game, get service, replicated storage, and then I'm going 76 00:04:40,430 --> 00:04:44,270 to get a variable for info, what I call that info message. 77 00:04:44,300 --> 00:04:51,830 Ari I probably should just copied it from the other one or a replicated storage wait for child info 78 00:04:51,830 --> 00:04:52,520 message. 79 00:04:52,550 --> 00:04:53,450 Ari. 80 00:04:54,230 --> 00:04:58,460 I'm also going to get a variable for my message queue. 81 00:04:58,520 --> 00:05:05,540 It's basically just going to be a table, but a queue is something where when you put messages in it, 82 00:05:05,540 --> 00:05:08,780 the first one out is going to be the first one in. 83 00:05:08,780 --> 00:05:11,930 That's why they call it a queue as opposed to a stack. 84 00:05:11,930 --> 00:05:17,600 Like if you stack something, the last thing on the stack is going to be the first thing you pull off. 85 00:05:17,600 --> 00:05:24,320 Or they say Pop, they pop, you pop something off the stack, but the queue, the first one in is going 86 00:05:24,320 --> 00:05:25,730 to be the first one out. 87 00:05:26,690 --> 00:05:31,980 Now I'm going to put messages in my message queue so we have our info message. 88 00:05:32,010 --> 00:05:34,700 R e that's firing messages off. 89 00:05:34,700 --> 00:05:43,920 We can capture them by doing our message info r e on client event connecting that to an anonymous function. 90 00:05:43,940 --> 00:05:49,220 If you remember on the other side, we passed in the player and the message the player is used to get 91 00:05:49,220 --> 00:05:49,630 to you. 92 00:05:49,640 --> 00:05:49,880 Right? 93 00:05:49,880 --> 00:05:54,710 This is a local script running locally on your computer, so we don't have the player here. 94 00:05:54,710 --> 00:05:57,620 We're just going to get the MSG the message. 95 00:05:57,620 --> 00:06:05,330 Then I'll use this table insert to insert my message into this table. 96 00:06:05,330 --> 00:06:10,130 And here's the message We didn't provide an index, so it's just going to put the message on the end. 97 00:06:10,370 --> 00:06:13,130 So this queue is going to continue to get messages. 98 00:06:13,130 --> 00:06:17,950 We need to process the messages or else it doesn't really pay to have a queue. 99 00:06:17,960 --> 00:06:27,050 So let's do a function local function process, MSG, and then we're going to do a while loop and I'll 100 00:06:27,050 --> 00:06:29,420 just put a weight in here. 101 00:06:29,420 --> 00:06:33,170 So we'll pull every 2 seconds, maybe every one second. 102 00:06:33,170 --> 00:06:34,520 We don't want to wait that long. 103 00:06:34,880 --> 00:06:42,680 And so while wait one second, do let's check for if there are messages in our queue, I'm going to 104 00:06:42,680 --> 00:06:50,180 look for the number of messages in our MSG queue check to see if it's greater than zero. 105 00:06:50,180 --> 00:06:56,030 If it is, I'm going to process a message and right now our processing just means printing it. 106 00:06:56,030 --> 00:07:01,370 So I'll do a client side process. 107 00:07:03,110 --> 00:07:08,060 MSG queue whoops, MSG queue bracket one. 108 00:07:08,060 --> 00:07:10,220 We're going to get the first message in the queue. 109 00:07:10,220 --> 00:07:20,690 Remember first in, first out, then we're going to remove we'll do a table, remove MSG, queue that 110 00:07:20,690 --> 00:07:23,360 first, that first message right there. 111 00:07:23,750 --> 00:07:27,230 And then the other condition that can happen is if the queue is empty. 112 00:07:27,230 --> 00:07:29,510 So if there's nothing in the queue. 113 00:07:31,120 --> 00:07:32,020 We'll do a print. 114 00:07:32,320 --> 00:07:42,970 The queue is empty and we need to call this, but we don't want it to interrupt anything. 115 00:07:42,970 --> 00:07:46,380 It's got an infinite while loop, so we have to be careful of that. 116 00:07:46,390 --> 00:07:53,440 Let's go down to the bottom and I think I'm just going to spawn process message in its own thread. 117 00:07:53,470 --> 00:07:56,680 So we're just going to spawn this off and then it's going to start running. 118 00:07:56,680 --> 00:07:59,590 It's not going to interfere with this script right here. 119 00:07:59,920 --> 00:08:02,470 All right, let's try it out and hit the play button. 120 00:08:03,070 --> 00:08:06,490 I'm going to go to my view tab and hit the output. 121 00:08:07,510 --> 00:08:14,470 Q is empty and boom, we got some messages and you can see that they printed out one a second after 122 00:08:14,470 --> 00:08:16,910 that and then the Q is empty again. 123 00:08:16,930 --> 00:08:18,120 That's pretty cool. 124 00:08:18,130 --> 00:08:25,540 Let's add a UI and then we can actually start displaying the message to the user in a way that would 125 00:08:25,540 --> 00:08:27,700 make sense for a player in a game.